package cn.gdut.chengcai.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gdut.chengcai.base.ConstantUtil;
import cn.gdut.chengcai.base.JsonUtil;
import cn.gdut.chengcai.base.MessageUtil;
import cn.gdut.chengcai.base.Pager;
import cn.gdut.chengcai.base.PagerUtil;
import cn.gdut.chengcai.base.PartnerUtil;
import cn.gdut.chengcai.base.ResponseData;
import cn.gdut.chengcai.entity.ChengcaiInfo;
import cn.gdut.chengcai.entity.FeedbackInfo;
import cn.gdut.chengcai.entity.PartnerInfo;
import cn.gdut.chengcai.entity.PartnerInfoExample;
import cn.gdut.chengcai.service.ChengcaiInfoService;
import cn.gdut.chengcai.service.FeedbackInfoService;
import cn.gdut.chengcai.service.MemberInfoService;
import cn.gdut.chengcai.service.PartnerInfoService;
import cn.gdut.chengcai.vo.PartnerInfoVo;

@Controller
@RequestMapping("/about")
public class AboutController {

	// public static LogUtil log =
	// LogUtil.getInstance(AboutController.class.getName());

	@Autowired
	protected ChengcaiInfoService chengcaiInfoService;

	@Autowired
	protected PartnerInfoService partnerInfoService;

	@Autowired
	protected FeedbackInfoService feedbackInfoService;

	@Autowired
	protected MemberInfoService memberInfoService;

	/**
	 * 企业介绍页
	 */

	@RequestMapping("/introduce")
	public String introduce(ModelMap map) {
		ChengcaiInfo entity = chengcaiInfoService.selectByPrimaryKey(1L);
		map.put("entity", entity);
		map.put("aboutUrl", "/about/introduce.html");
		return "/about/introduce";
	}

	/**
	 * 合作伙伴页
	 */

	@RequestMapping("/partner")
	public String partner(HttpSession session, ModelMap map) {

		PartnerInfoExample example = new PartnerInfoExample();
		example.createCriteria().andIsDeletedEqualTo(false).andIsShowEqualTo(true);
		example.setOrderByClause("create_time_  desc");
		List<PartnerInfo> listTotal = partnerInfoService.selectByExample(example);
		Long itemCount = Long.valueOf(String.valueOf(listTotal.size()));
		Pager pager = PagerUtil.getPager(session, itemCount);
		int fromIndex = (pager.getPageNo() - 1) * pager.getPageSize();
		if (fromIndex >= listTotal.size()) {
			fromIndex = 0;
			pager.setPageNo(1);
			session.setAttribute(ConstantUtil.STR_PAGE_NO, "1");
		}
		int toIndex = fromIndex + pager.getPageSize();
		if (toIndex >= listTotal.size()) {
			toIndex = listTotal.size();
		}
		List<PartnerInfo> list = listTotal.subList(fromIndex, toIndex);
		List<PartnerInfoVo> voList = new ArrayList<>();
		try {
			PartnerUtil.fillVoList(list, voList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("list", voList);
		map.put("aboutUrl", "/about/partner.html");
		session.setAttribute("pager", pager);
		return "/about/partner";
	}

	/**
	 * 合作伙伴申请加入页
	 */
	/*
	
	@RequestMapping("/partner/apply")
	public String partnerApply(ModelMap map, HttpSession session) {
	return "/about/partnerApply";
	}
	
	*//**
		 * 合作伙伴申请加入操作
		 *//*
		
		@RequestMapping(value = "/partner/applyAction", method = RequestMethod.POST)
		@ResponseBody
		public ResponseData<Object> partnerApplyAction(HttpServletRequest request, HttpSession session) {
		ResponseData<Object> result = new ResponseData<>();
		result.setIsSuccess(false);
		try {
		Date createTime = new Date();
		PartnerInfo entity = new PartnerInfo();
		entity.setName(RequestUtil.getParameter(request, "name"));
		entity.setIntro(RequestUtil.getParameter(request, "intro"));
		entity.setHomepage(RequestUtil.getParameter(request, "homepage"));
		entity.setLinkman(RequestUtil.getParameter(request, "linkman"));
		entity.setContactNumber(RequestUtil.getParameter(request, "contactNumber"));
		entity.setContactEmail(RequestUtil.getParameter(request, "contactEmail"));
		entity.setPostscript(RequestUtil.getParameter(request, "postscript"));
		
		// 上传处理
		Map<Integer, UploadInfoEntity> map = UploadDownloadUtil.uploadAction(request, session,
		UploadFileType.APPLY.getValue(), createTime);
		if (null != map && map.size() != 0) {
		
		String filename = "";
		if (null != map.get(1)) {
		filename = entity.getLogo();
		if (StringUtils.isEmpty(filename) == false) {
		UploadDownloadUtil.deleteAction(filename);
		}
		entity.setLogo(map.get(1).getFileUrl());
		}
		
		if (null != map.get(2)) {
		filename = entity.getAttachment();
		if (StringUtils.isEmpty(filename) == false) {
		UploadDownloadUtil.deleteAction(filename);
		}
		entity.setAttachment(map.get(2).getFileUrl());
		}
		
		}
		
		PartnerUtil.initEntity(entity, createTime);
		partnerInfoService.insert(entity);
		result.setIsSuccess(true);
		} catch (Exception e) {
		result.setMessage(MessageUtil.SYSTEM_ERROR);
		// log.error(e.getMessage(), e);
		}
		return result;
		}*/

	/**
	 * 联系我们页
	 */

	@RequestMapping("/contact")
	public String contact(ModelMap map) {
		ChengcaiInfo entity = chengcaiInfoService.selectByPrimaryKey(1L);
		map.put("entity", entity);
		map.put("aboutUrl", "/about/contact.html");
		return "/about/contact";
	}

	/**
	 * 意见反馈页
	 */

	@RequestMapping("/feedback")
	public String feedback(ModelMap map) {
		map.put("aboutUrl", "/about/feedbackInfo.html");
		return "/about/feedback";
	}

	/**
	 * 意见反馈提交操作
	 */

	@RequestMapping(value = "/feedbackAction", method = RequestMethod.POST)
	@ResponseBody
	public ResponseData<Object> feedbackAction(@RequestBody String jsonString, HttpServletRequest request,
			HttpSession session) {
		ResponseData<Object> result = new ResponseData<>();
		result.setIsSuccess(false);
		try {
			FeedbackInfo entity = (FeedbackInfo) JsonUtil.fromJson(jsonString, FeedbackInfo.class);
			entity.setIsReply(false);
			entity.setReplyOpinion(null);
			entity.setReplyResult(null);
			entity.setReplyOperator(null);
			entity.setReplyTime(null);
			entity.setIsDeleted(false);
			entity.setCreateTime(new Date());
			feedbackInfoService.insert(entity);
			result.setMessage(MessageUtil.FEEDBACK_IS_SUBMIT);
			result.setIsSuccess(true);
		} catch (Exception e) {
			result.setMessage(MessageUtil.SYSTEM_ERROR);
			// log.error(e.getMessage(), e);
		}
		return result;
	}

}